//
//  UIButton + Extension.swift
//  Weibo10
//
//  Created by 小k on 15/11/4.
//  Copyright © 2015年 itheima. All rights reserved.
//

import UIKit

extension UIButton {
   
    ///  便利构造函数创建按钮
    ///
    ///  - parameter imageName:     按钮图片名称
    ///  - parameter backImageName: 背景图片名称
    ///
    ///  - returns: UIButton
    convenience init(imageName:String, backImageName:String?) {
        
     // 调用父类, 保证对象创建完成
        self.init()
        
         setImage(UIImage(named: imageName), forState: UIControlState.Normal)
         setImage(UIImage(named: imageName + "_highlighted"), forState: UIControlState.Highlighted)
        
        if let backImageName = backImageName {
         setBackgroundImage(UIImage(named: backImageName), forState: UIControlState.Normal)
         setBackgroundImage(UIImage(named: backImageName + "_highlighted"), forState: UIControlState.Highlighted)
        }
        
        // 会根据背景图片的大小调整尺寸
         sizeToFit()
        
    }
    
    ////  便利构造函数创建按钮
    ///
    ///  - parameter title:     按钮标题
    ///  - parameter imageName: 图片名称
    ///
    ///  - returns: UIButton
    convenience init (title:String, color:UIColor, imageName:String) {
        // 调用父类
        self.init()

        setTitle(title, forState: UIControlState.Normal)
        setTitleColor(color, forState: UIControlState.Normal)
        
        setBackgroundImage(UIImage(named: imageName), forState: UIControlState.Normal)
        
    }
    
    /// 便利构造函数
    ///
    /// - parameter title:     title
    /// - parameter color:     color
    /// - parameter fontSize:  字体大小
    /// - parameter imageName: 图像名称
    ///
    /// - returns: UIButton
    convenience init(title: String, fontSize: CGFloat, color: UIColor, imageName: String) {
        self.init()
        
        setTitle(title, forState: .Normal)
        setTitleColor(color, forState: .Normal)
        setImage(UIImage(named: imageName), forState: .Normal)
        
        titleLabel?.font = UIFont.systemFontOfSize(fontSize)
        
        sizeToFit()
    }
    
}
